package com.disapand.strongarmy.controller

import com.disapand.strongarmy.service.*
import com.disapand.strongarmy.utils.*
import com.disapand.strongarmy.vo.*
import org.springframework.web.bind.annotation.*
import java.time.*

/**
 *
 * @auth disapand
 * @date 2021/11/5 - 22:32 - 星期五
 *
 */
@RestController
@RequestMapping("/menus", name = "菜单管理")
class MenuController(private val menuService: MenuService) {

    @PostMapping("/add", name = "新增菜单")
    fun add(
        @RequestParam("name") name: String,
        @RequestParam("path") path: String,
        @RequestParam("icon", required = false) icon: String?,
        @RequestParam("parent_id", required = false, defaultValue = "0") parentId: Int,
        @RequestParam("weight", required = false, defaultValue = "9999") weight: Int
    ): ResponseVO {
        val result = menuService.add(name, path, icon, parentId, weight)
        return ResponseVO.ofSuccess(result)
    }

    /**
     * 以树形返回所有菜单
     */
    @get:GetMapping("/tree", name = "树形获取所有菜单")
    val getTree: ResponseVO
        get() {
            val allMenus = menuService.get()
            return ResponseVO.ofSuccess(SaUtils.generateMenuTree(allMenus))
        }

    /**
     * 分页返回菜单列表
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/list", name = "分页获取菜单列表")
    fun menus(
        @RequestParam("name", required = false) name: String?,
        @RequestParam("path", required = false) path: String?,
        @RequestParam("parent_id", required = false) parentId: Int?,
        @RequestParam("status", required = false) status: Int?,
        @RequestParam("created_by", required = false) createdBy: Int?,
        @RequestParam("created_start_at", required = false) createdStartAt: LocalDateTime?,
        @RequestParam("created_end_at", required = false) createdEndAt: LocalDateTime?,
        @RequestParam("updated_by", required = false) updatedBy: Int?,
        @RequestParam("updated_start_at", required = false) updatedStartAt: LocalDateTime?,
        @RequestParam("updated_end_at", required = false) updatedEndAt: LocalDateTime?,
        @RequestParam("current", defaultValue = "1", required = false) current: Int,
        @RequestParam("page_size", defaultValue = "20", required = false) pageSize: Int
    ): ResponseVO {
        val result = menuService.get(
            name,
            path,
            parentId,
            status,
            createdBy,
            createdStartAt,
            createdEndAt,
            updatedBy,
            updatedStartAt,
            updatedEndAt,
            current,
            pageSize
        )
        return ResponseVO.ofSuccess(result)
    }

    /**
     * 根据 id 获取菜单
     *
     * @param id
     * @return
     */
    @GetMapping("/get", name = "根据 id 获取菜单")
    fun get(
        @RequestParam("id") id: Int
    ): ResponseVO {
        return ResponseVO.ofSuccess(menuService.get(id))
    }

    /**
     * 根据 id 删除菜单name
     *
     * @param id
     * @return
     */
    @DeleteMapping("/del", name = "删除指定的菜单")
    fun delete(
        @RequestParam("ids") ids: IntArray
    ): ResponseVO {
        return ResponseVO.ofSuccess(menuService.deleted(ids))
    }

    /**
     * 编辑菜单
     *
     * @param id
     * @param name
     * @param path
     * @param icon
     * @param parentId
     * @param weight
     * @return
     */
    @PutMapping("/edit", name = "编辑菜单")
    fun edit(
        @RequestParam("id") id: Int,
        @RequestParam("name", required = false) name: String?,
        @RequestParam("path", required = false) path: String?,
        @RequestParam("icon", required = false) icon: String?,
        @RequestParam("parent_id", required = false) parentId: Int?,
        @RequestParam("weight", required = false) weight: Int?,
    ): ResponseVO {
        val result = menuService.edit(id, name, path, icon, parentId, weight)
        return ResponseVO.ofSuccess(result)
    }
}